package com.example.fragment;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.example.R;
import com.example.model.EventBean;
import com.example.util.Constant;
import com.example.widget.X5WebView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页Fragment
 */
public class IndexFragment extends Fragment {
    @BindView(R.id.web_view_index)
    X5WebView webView;

    private View rootView;
    private ValueCallback<Uri> uploadFile;
    private ValueCallback<Uri[]> uploadFiles;
    private String loadUrl = Constant.H5_URL + "index";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView==null){
            rootView =inflater.inflate(R.layout.fragment_index,null,false);
            ButterKnife.bind(this,rootView);
            EventBus.getDefault().register(this);
        }
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initWebView();
    }

    /**
     *
     */
    private void initWebView(){
        webView.setWebViewClient(new WebViewClient() {
            public void OpenfileChooser(ValueCallback<Uri> uploadMsg) {
                uploadFile = uploadMsg;
                openFileChooseProcess();
            }

            public void OpenfileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                uploadFile = uploadMsg;

                openFileChooseProcess();
            }

            public void OpenfileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                uploadFile = uploadMsg;
                openFileChooseProcess();
            }

            public boolean OnShowfileChooser(com.tencent.smtt.sdk.WebView webView,
                                             ValueCallback<Uri[]> filepathcallback,
                                             WebChromeClient.FileChooserParams fileChooserParams
            ) {

                uploadFiles = filepathcallback;
                openFileChooseProcess();
                return true;
            }
        });

        webView.getSettings().setUseWideViewPort(true);
        webView.loadUrl(loadUrl);

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                webView.evaluateJavascript("document.getElementsByTagName('body')[0].getElementsByClassName('kz-float-layer bottom')[0].remove();",
                        new ValueCallback<String>() {
                            //加载网页结束之后的
                            @Override
                            public void onReceiveValue(String s) {
                                Log.e("zz", s);
                            }
                        });

            }


            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            //加载进度
            @Override
            public void onProgressChanged(WebView webView, int i) {
                super.onProgressChanged(webView, i);
                Log.e("zz", String.valueOf(i));
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventBean bean) {
        webView.loadUrl(loadUrl);
        webView.loadUrl( "javascript:window.location.reload( true )" );
    }

    /**
     *
     */
    private void openFileChooseProcess() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        startActivityForResult(Intent.createChooser(intent, "test"), 0);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (null != uploadFile) {
                Uri result = data == null || resultCode != RESULT_OK ? null
                        : data.getData();
                uploadFile.onReceiveValue(result);
                uploadFile = null;
            }
            if (null != uploadFiles) {
                Uri result = data == null || resultCode != RESULT_OK ? null
                        : data.getData();
                uploadFiles.onReceiveValue(new Uri[]{result});
                uploadFiles = null;
            }

        } else if (resultCode == RESULT_CANCELED) {
            if (null != uploadFile) {
                uploadFile.onReceiveValue(null);
                uploadFile = null;
            }

        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(webView!=null){
            webView.destroy();
        }

        EventBus.getDefault().unregister(this);
    }
}
